#coding:utf-8
import configparser
import base64
from tool import api
from logger import Logger
from logger import set_log_filename
import datetime

today = str(datetime.date.today())
logger = Logger("Account")
set_log_filename(today)

class Account(object):
    """
    账户管理
    """
    path =api.data_path / "account.ini"
    conf = configparser.ConfigParser()
    conf.read(path, encoding="utf-8")

    #新建ini文件
    def set_ini(self):
        '''
        新建用户配置
        '''
        userid = str(base64.b64encode(input('请输入学号').encode('utf-8')), 'utf-8')
        password = str(base64.b64encode(input('请输入密码').encode('utf-8')), 'utf-8')
        self.conf.add_section('user1')
        self.conf.set('user1', 'userid', userid)
        self.conf.set('user1', 'password', password)
        with open(self.path, 'w') as file:
            self.conf.write(file)
            file.close()
        logger.info("新建用户配置")
        return '用户信息已保存，保存在user1'

    #添加用户信息
    def add_ini(self):
        '''
        添加用户信息
        '''
        if self.path.exists():
            i = 1
            section_list = self.conf.sections()
            while True:
                section_name = f'user{i}'.format(i=i)
                if section_name in section_list:
                    i += 1
                else:
                    userid = str(base64.b64encode(input('请输入学号').encode('utf-8')), 'utf-8')
                    password = str(base64.b64encode(input('请输入密码').encode('utf-8')), 'utf-8')
                    self.conf.add_section(section_name)
                    self.conf.set(section_name,'userid',userid)
                    self.conf.set(section_name,'password',password)
                    with open(self.path, 'w') as file:
                        self.conf.write(file)
                        file.close()
                    j = i
                    break
            logger.info(f'添加用户user{j}')
            return f'用户信息已保存，保存在user{j}'
        else:
            logger.warning('配置文件不存在')
            return '配置文件不存在'

    #删除指定用户
    def del_ini(self,section: str):
        '''
        删除指定用户
        :param section: 账户
        '''
        if self.path.exists():
            section_list = self.conf.sections()
            if section in section_list:
                self.conf.remove_section(section)
                with open(self.path, 'w') as file:
                    self.conf.write(file)
                    file.close()
                logger.info(f'已删除用户{section}')
                return f'已删除用户{section}'
            else:
                logger.info(f'未找到用户{section}')
                return f'未找到用户{section}'

        else:
            logger.warning('配置文件不存在')
            return '配置文件不存在'

    #修改用户信息
    def modify_ini(self,section: str):
        '''
        修改用户信息
        :param section: 账户
        '''
        if self.path.exists():
            section_list = self.conf.sections()
            if section in section_list:
                option_list = self.conf.options(section)
                for i in option_list:
                    print(i)
                option = str(input('请选择需要修改的信息'))
                if option in option_list:
                    option_value = str(input('请输入修改内容'))
                    option_value = str(base64.b64encode(option_value.encode('utf-8')), 'utf-8')
                    self.conf.set(section, option, option_value)
                    with open('account.ini', 'w') as file:
                        self.conf.write(file)
                        file.close()
                    logger.info(f'已修改用户{section}的{option}信息')
                    return f'已修改用户{section}的{option}信息'
                else:
                    logger.info(f'未找到{option}')
                    return f'未找到{option}'
            else:
                logger.info(f'未找到{section}')
                return f'未找到{section}'
        else:
            logger.warning('配置文件不存在')
            return '配置文件不存在'

    # 获取账号信息
    def obtain_option(self,section):
        '''
        获取账号信息
        :param section: 账户
        '''
        if self.path.exists():
            userid_en = self.conf[section]['userid']
            password_en = self.conf[section]['password']
            userid_de = str(base64.b64decode(userid_en))
            password_de = str(base64.b64decode(password_en))
            account = [userid_de[2:-1], password_de[2:-1]]
            logger.info(f'获取{section}信息')
            return account
        else:
            logger.warning('配置文件不存在')
            return '配置信息不存在'

    #获取账户信息
    def obtain_section(self):
        '''
        获取账户信息
        '''
        if self.path.exists():
            section_list = self.conf.sections()
            j = 0
            for i in section_list:
                print(i)
                j += 1
            return j
        else:
            logger.warning('配置文件不存在')
            return '配置信息不存在'